package com.concerneyes.rentbar;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.rentbar.dao.ShoppingCartBean;
import com.concerneyes.rentbar.listener.OnShoppingCartChangeListener;
import com.concerneyes.rentbar.listener.ResponseCallBack;
import com.concerneyes.rentbar.listener.ShoppingCartHttpBiz;
import com.concerneyes.rentbar.util.ShoppingCartBiz;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import common.AbsBaseView;

/**
 * 租Bar购物车的实现F
 * TODO 由于项目需求调整本包下的 rentbar包下所有类废弃 将在最后的项目结构优化中删除
 */

public class RentBarView extends AbsBaseView implements View.OnClickListener, OnShoppingCartChangeListener {
    private ListView lv_rentbar;
    private ImageView ivAgree, ivChooseAll;
    private TextView tvAgree, tvTotal, tvBalance;
    private LinearLayout llChooseAll;
    private RentBarAdapter adapter;
    private List<ShoppingCartBean> mListGoods = new ArrayList<>();
    private boolean isCleckAll = true;
    private List<Integer> totlePrice;

    public RentBarView(Context context) {
        super(context);
        initView();
    }

    @Override
    public int layoutResId() {
        return R.layout.activity_rentbar;
    }

    @Override
    public void loadData(Object data) {

    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    private void initView() {
        lv_rentbar = (ListView) findViewById(R.id.lv_rentbar);
        ivAgree = (ImageView) findViewById(R.id.iv_agree);
        ivChooseAll = (ImageView) findViewById(R.id.iv_choose_all);
        tvAgree = (TextView) findViewById(R.id.tv_agree);
        tvTotal = (TextView) findViewById(R.id.tv_total);
        tvBalance = (TextView) findViewById(R.id.tv_balance);
        llChooseAll = (LinearLayout) findViewById(R.id.ll_choose_all);
        requestShoppingCartList();
        setAdapter();
    }

    /**
     * 获取购物车列表的数据（数据和网络请求也是非通用部分）
     */
    private void requestShoppingCartList() {
        ShoppingCartBiz.delAllGoods();
//        testAddGood();
        //使用本地JSON，作测试用。本来应该是将商品ID发送的服务器，服务器返回对应的商品信息；
        ShoppingCartHttpBiz.requestOrderList(getContext(), new ResponseCallBack() {//requestOrderList(list, new VollyHelperNew.ResponseCallBack())
            public void handleResponse(Object o, int code) {
                mListGoods = ShoppingCartHttpBiz.handleOrderList((JSONObject) o, code);
                adapter = new RentBarAdapter(getActivity(), mListGoods);
                lv_rentbar.setAdapter(adapter);
                adapter.setOnShoppingCartChangeListener(RentBarView.this);
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void setAdapter() {
        //通过监听器关联Activity和Adapter的关系，解耦；
        View.OnClickListener listener = adapter.getAdapterListener();
        if (listener != null) {
            //即使换了一个新的Adapter，也要将“全选事件”传递给adapter处理；
            ivChooseAll.setOnClickListener(adapter.getAdapterListener());
            //结算时，一般是需要将数据传给订单界面的
            tvBalance.setOnClickListener(adapter.getAdapterListener());
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDataChange(String selectCount, String selectMoney) {
        int goodsCount = ShoppingCartBiz.getGoodsCount();
        if (goodsCount == 0) {
//            showEmpty(true);
        }
        String countGoods = String.format(getResources().getString(R.string.count_goods), selectCount);
        tvTotal.setText(selectMoney);
        tvBalance.setText(countGoods);
    }

    @Override
    public void onSelectItem(boolean isSelectedAll) {
        ShoppingCartBiz.checkItem(isSelectedAll, ivChooseAll);
    }

    private void testAddGood() {
        ShoppingCartBiz.addGoodToCart("279457f3-4692-43bf-9676-fa9ab9155c38", "6");
        ShoppingCartBiz.addGoodToCart("95fbe11d-7303-4b9f-8ca4-537d06ce2f8a", "8");
        ShoppingCartBiz.addGoodToCart("8c6e52fb-d57c-45ee-8f05-50905138801b", "9");
        ShoppingCartBiz.addGoodToCart("7d6e52fb-d57c-45ee-8f05-50905138801d", "3");
        ShoppingCartBiz.addGoodToCart("7d6e52fb-d57c-45ee-8f05-50905138801e", "3");
        ShoppingCartBiz.addGoodToCart("7d6e52fb-d57c-45ee-8f05-50905138801f", "3");
        ShoppingCartBiz.addGoodToCart("7d6e52fb-d57c-45ee-8f05-50905138801g", "3");
        ShoppingCartBiz.addGoodToCart("7d6e52fb-d57c-45ee-8f05-50905138801h", "3");
    }

}
